home *** CD-ROM | disk | FTP | other *** search
/ PC Direct 1998 August / PC Direct August 1998.iso / S / powerj / Product / hpp.z / WLISTBOX.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-25  |  17.8 KB  |  524 lines

  1. /*************************************************************************
  2.  *
  3.  * WListBox -- Wrapper for the Windows 95 Listbox control.
  4.  *
  5.  *   Events:
  6.  *
  7.  *       BeginOwnerDraw -- Called before the first DrawItem.
  8.  *
  9.  *       DataAvailable --
  10.  *
  11.  *       DataClose --
  12.  *
  13.  *       DataOpen --
  14.  *
  15.  *       DataRequest --
  16.  *
  17.  *       DoubleClick -- When a listbox item has been double-clicked
  18.  *
  19.  *       DrawItem -- Called for owner-draw.
  20.  *
  21.  *       EndOwnerDraw -- Called after last DrawItem.
  22.  *
  23.  *       Select -- When a listbox item has been selected
  24.  *
  25.  *************************************************************************/
  26.  
  27. #ifndef _WLISTBOX_HPP_INCLUDED
  28. #define _WLISTBOX_HPP_INCLUDED
  29.  
  30. #ifndef _WNO_PRAGMA_PUSH
  31. #pragma pack(push,8);
  32. #pragma enum int;
  33. #endif
  34.  
  35. #ifndef _WCONTROL_HPP_INCLUDED
  36. #  include "wcontrol.hpp"
  37. #endif
  38. #ifndef _WSTRING_HPP_INCLUDED
  39. #  include "wstring.hpp"
  40. #endif
  41. #ifndef _WDATATRG_HPP_INCLUDED
  42. #  include "wdatatrg.hpp"
  43. #endif
  44. #ifndef _WLOCALE_HPP_INCLUDED
  45. #  include "wlocale.hpp"
  46. #endif
  47. #ifndef _WDRAWITM_HPP_INCLUDED
  48. #  include "wdrawitm.hpp"
  49. #endif
  50.  
  51. class WDataSource;
  52.  
  53. //
  54. // ListBox styles
  55. //
  56.  
  57. #define WLBSDefault           ((WStyle)0x50A00141L) // WS_VISIBLE|LBS_HASSTRINGS|LBS_NOTIFY|WS_BORDER|WS_VSCROLL|WS_CHILD|LBS_NOINTEGRALHEIGHT
  58.  
  59. #define WLBSSingleSel         ((WStyle)0x00000000L) // WS_OVERLAPPED
  60. #define WLBSNotify            ((WStyle)0x00000001L) // LBS_NOTIFY
  61. #define WLBSSort              ((WStyle)0x00000002L) // LBS_SORT
  62. #define WLBSNoRedraw          ((WStyle)0x00000004L) // LBS_NOREDRAW
  63. #define WLBSMultipleSel       ((WStyle)0x00000008L) // LBS_MULTIPLESEL
  64. #define WLBSOwnerDrawFixed    ((WStyle)0x00000010L) // LBS_OWNERDRAWFIXED
  65. #define WLBSOwnerDrawVariable ((WStyle)0x00000020L) // LBS_OWNERDRAWVARIABLE
  66. #define WLBSHasStrings        ((WStyle)0x00000040L) // LBS_HASSTRINGS
  67. #define WLBSUseTabStops       ((WStyle)0x00000080L) // LBS_USETABSTOPS
  68. #define WLBSNoIntegralHeight  ((WStyle)0x00000100L) // LBS_NOINTEGRALHEIGHT
  69. #define WLBSMultiColumn       ((WStyle)0x00000200L) // LBS_MULTICOLUMN
  70. #define WLBSWantKeyboardInput ((WStyle)0x00000400L) // LBS_WANTKEYBOARDINPUT
  71. #define WLBSExtendedSel       ((WStyle)0x00000800L) // LBS_EXTENDEDSEL
  72. #define WLBSDisableNoScroll   ((WStyle)0x00001000L) // LBS_DISABLENOSCROLL
  73. #define WLBSNoData            ((WStyle)0x00002000L) // LBS_NODATA
  74. #define WLBSNoSel             ((WStyle)0x00004000L) // LBS_NOSEL
  75.  
  76. #define WLBSSorted            WLBSSort
  77. #define WLBSMultipleSelection WLBSMultipleSel
  78. #define WLBSNoSelection       WLBSNoSel
  79.  
  80.  
  81. class WCMCLASS WListBox : public WControl {
  82.     WDeclareSubclass( WListBox, WControl );
  83.  
  84.     public:
  85.         /**************************************************************
  86.          * Constructors and destructors
  87.          **************************************************************/
  88.  
  89.         WListBox();
  90.  
  91.         ~WListBox();
  92.  
  93.         /**************************************************************
  94.          * Properties
  95.          **************************************************************/
  96.  
  97.         // ColumnWidth
  98.         //
  99.         //     Use to set the width in pixels of the columns in a
  100.         //     multi-column listbox.  A value of -1 means no width
  101.         //     has been set.
  102.  
  103.         WInt  GetColumnWidth() const;
  104.         WBool SetColumnWidth( WInt width );
  105.  
  106.         // Count
  107.         //
  108.         //     Returns the number of items in the list box, or negative
  109.         //     if an error occurs.
  110.  
  111.         WInt GetCount() const;
  112.  
  113.         // DataBindAsLookup
  114.         //
  115.         //     DataTarget property
  116.  
  117.         WBool GetDataBindAsLookup() const;
  118.         WBool SetDataBindAsLookup( WBool bindAsLookup );
  119.  
  120.         // DataColumns
  121.         //
  122.         //     DataTarget property
  123.  
  124.         WString GetDataColumns() const;
  125.         WBool   SetDataColumns( const WString & cols );
  126.  
  127.         // DataLookupColumns
  128.         //
  129.         //     DataTarget property
  130.  
  131.         WString GetDataLookupColumns() const;
  132.         WBool   SetDataLookupColumns( const WString & cols );
  133.  
  134.         // DataLookupSource
  135.         //
  136.         //     DataTarget property
  137.  
  138.         WDataSource *GetDataLookupSource() const;
  139.         WBool        SetDataLookupSource( WDataSource *source );
  140.  
  141.         // DataSource
  142.         //
  143.         //     DataTarget property
  144.  
  145.         WDataSource *GetDataSource() const;
  146.         WBool        SetDataSource( WDataSource *source );
  147.  
  148.         // DataTrackRow
  149.         //
  150.         //     DataTarget method
  151.  
  152.         WBool GetDataTrackRow() const;
  153.         WBool SetDataTrackRow( WBool trackRow );
  154.  
  155.         // HorizontalExtent
  156.         //
  157.         //     The width, in pixels, by which a listbox can be scrolled
  158.         //     horizontally (the scrollable width).  If the value is set
  159.         //     to -1, the horizontal extent will be calculated automatically
  160.         //     on a one-time only basis based on the strings currently in
  161.         //     the listbox.
  162.  
  163.         WBool SetHorizontalExtent( WInt horizontalExtent );
  164.         WInt  GetHorizontalExtent() const;
  165.  
  166.         // FDXDataSource
  167.  
  168.         WBool SetFDXDataSource( WStringArray * fdxDataSource );
  169.         WBool SetFDXDataSource( WIntArray * _fdxDataSourceIntArray );
  170.         WBool SetFDXDataSource( WInt * fdxDataSource );
  171.         WBool SetFDXDataSource( WBool * fdxDataSource );
  172.  
  173.         // Selected
  174.         //
  175.         //     For single-select listboxes, GetSelected() returns the index
  176.         //     of the currently-selected item, or -1 if no selection,
  177.         //     and SetSelected sets the current selection.  SetSelected
  178.         //     of -1 clears the selection.
  179.         //
  180.         //     For multi-select listboxes, use GetSelected( WInt ) to
  181.         //     determine if a specific index is selected.  Use 
  182.         //     SetSelected to select or deselect an item, or use index
  183.         //     -1 to select or deselect all items.
  184.         //     Note: The multi-select versions of GetSelected and
  185.         //           SetSelected are Item Properties, not Properties.
  186.  
  187.         WInt  GetSelected() const;
  188.         WBool SetSelected( WInt index );
  189.  
  190.         // SelectedCount
  191.         //
  192.         //     Returns the number of items selected in a multi-selection
  193.         //     listbox, or -1 if an error occurs.
  194.  
  195.         WInt GetSelectedCount() const;
  196.  
  197.         // SelectedList
  198.         //
  199.         //     An array of indices of selected items.  If no items
  200.         //     are selected, the array size is zero.
  201.  
  202.         WIntArray GetSelectedList() const;
  203.  
  204.         // SendDeleteItem
  205.         //
  206.         //     If TRUE, DeleteItem events will be triggered consistently
  207.         //     across all platforms if non-null user data has been
  208.         //     set for any items in the listbox.
  209.  
  210.         WBool GetSendDeleteItem() { return _sendDeleteItem; }
  211.         WBool SetSendDeleteItem( WBool set ) { _sendDeleteItem = set; return TRUE; }
  212.  
  213.         // Sort
  214.         //
  215.         //     Get/set the sort mode.
  216.  
  217.         WBool GetSort() const;
  218.  
  219.         WBool SetSort( WBool sort, WBool clone=FALSE );
  220.  
  221.         // Locale (for type of sort)
  222.         //
  223.         //
  224.         WBool           SetLocale( WLocaleID locID );
  225.         WLocaleID       GetLocale();
  226.  
  227.         // TopIndex
  228.         //
  229.         //     Get/set the top index.
  230.  
  231.         WInt GetTopIndex() const;
  232.         WBool SetTopIndex( WInt index );
  233.  
  234.         /**************************************************************
  235.          * Methods
  236.          **************************************************************/
  237.  
  238.         // Add
  239.         //
  240.         //     Adds an item.  If itemNumber is set to >= 0, item is
  241.         //     inserted at the given location.  If the list is sorted
  242.         //     and itemNumber is set to -1, inserts in correct spot;
  243.         //     otherwise adds to the end.  Can also set optional
  244.         //     per-item data.
  245.         //     Returns the index of the item, or a negative value in
  246.         //     case of error.
  247.  
  248.         WInt Add( const WChar * string, WInt itemNumber=-1,
  249.                   void * itemUserData=NULL, WBool addInSortedOrder=TRUE );
  250.  
  251.         // DataAddLookupItem
  252.         //
  253.         //     Allows the user to manually fill the list with lookup items.
  254.         //     This is an alternative method to setting the DataLookupSource
  255.         //     and DataLookupColumns properties.
  256.  
  257.         WInt DataAddLookupItem( const WDataValue & value,
  258.                                 const WChar * string = NULL,
  259.                                 WInt itemNumber=-1, void * itemUserData=NULL,
  260.                                 WBool addInSortedOrder=TRUE );
  261.  
  262.         // DataFetchLookupItems
  263.         //
  264.         //      Forces the lookup items to be fetched from the database
  265.         //      (according to the DataLookupSource and DataLookupColumns
  266.         //      properties).
  267.         //      This is normally done when either data source is opened...
  268.  
  269.         WBool DataFetchLookupItems();
  270.  
  271.         // Delete
  272.         //
  273.         //     Deletes the specified item.  If an item's data is set,
  274.         //     invokes the WDeleteItemEvent event first.  Returns the count
  275.         //     of items remaining in the list, or negative if error occurs.
  276.  
  277.         WInt Delete( WInt itemNumber );
  278.  
  279.         // DeleteAll
  280.  
  281.         WBool DeleteAll();
  282.  
  283.         // Find
  284.         //
  285.         //     Finds the first string that matches a given prefix.  The
  286.         //     search starts immediately AFTER the given index (use -1
  287.         //     to search from the beginning) and wraps when the bottom
  288.         //     is reached.  The search is not case-sensitive.  If exact
  289.         //     is specified, the string must match exactly, otherwise
  290.         //     a prefix match is done.  Returns the index of the match
  291.         //     or negative on error.
  292.  
  293.         WInt Find( const WChar * match, WInt startAfter=-1,
  294.                    WBool exact=FALSE ) const;
  295.  
  296.         // MakeDraggable
  297.  
  298.         WBool MakeDraggable();
  299.  
  300.         // Reset
  301.         //
  302.         //     Reset (clear) the list box.
  303.  
  304.         void Reset();
  305.  
  306.         // Retrieve
  307.         //
  308.         //     Return the index of the listbox item closest to a given point
  309.  
  310.         WLong Retrieve( const WPoint & pt ) const;
  311.  
  312.         // Select
  313.         //
  314.         //     Find and select an item.  Searches for the first item whose
  315.         //     prefix matches (case-insensitive) the given string.  Returns
  316.         //     the index of the new selected item, or negative if an error
  317.         //     occurs.  If startAfter is -1, search starts at first item.
  318.  
  319.         WInt Select( const WChar * match, WInt startAfter=-1 );
  320.  
  321.         /**************************************************************
  322.          * Item Properties
  323.          **************************************************************/
  324.  
  325.         // DataLookupValue
  326.         //
  327.         //     Get the lookup value associated with the given list item.
  328.  
  329.         WDataValue GetDataLookupValue( WInt itemNumber ) const;
  330.  
  331.         // Height
  332.         //
  333.         //     Get/set height of listbox items. For get, negative value
  334.         //     is returned to indicate error.
  335.         // NOTE: Low item heights lead to particularily ugly results.
  336.  
  337.         WInt GetHeight( WInt itemNumber ) const;
  338.         WBool SetHeight( WInt itemNumber, WInt height );
  339.  
  340.         // Text
  341.         //
  342.         //     Get the text of a specific item in the list.  First two
  343.         //     versions return a WString, which is set to a null string
  344.         //     if an error occurs.  Last version copies it into a buffer
  345.         //     and returns the length, or a negative value.
  346.  
  347.         WString GetText( WInt itemNumber ) const;
  348.  
  349.         // Selected
  350.         //
  351.         //     c.f. Selected Property
  352.  
  353.         WInt  GetSelected( WInt whichItem ) const;
  354.         WBool SetSelected( WInt index, WBool selected );
  355.  
  356.         // TextLength
  357.         //
  358.         //     Get the length of the text of an item in the list (without
  359.         //     the null character).  A negative value is returned in case
  360.         //     of error.
  361.  
  362.         WInt GetTextLength( WInt itemNumber ) const;
  363.  
  364.         // UserData
  365.         //
  366.         //     Get/set per-item data.  When getting data, a value of
  367.         //     ((void *)-1) for the itemUserData indicates an error.
  368.  
  369.         virtual WBool SetUserData( WInt itemNumber, void * itemUserData );
  370.         virtual void * GetUserData( WInt itemNumber ) const;
  371.  
  372.         /**************************************************************
  373.          * Overrides
  374.          **************************************************************/
  375.  
  376.         virtual WColor GetBackColor( WBool getResultingColor=TRUE ) const;
  377.  
  378.         virtual size_t GetTextLength() const;
  379.  
  380.         virtual WBool WantsDragMessage( WULong whichButton ) const;
  381.  
  382.         virtual WBool FillDragEventData( WDragEventData & event ) const;
  383.  
  384.         virtual WBool ProcessCommand( WUInt id, WNotify code,
  385.                                       WNotifyInfo info, WLong & returns );
  386.  
  387.         virtual WBool CloneWindow( WStyle newStyle, WStyle newExStyle,
  388.                                    void * data=NULL );
  389.  
  390.         virtual WBool ProcessMessage( const WMessage & msg,
  391.                                       WLong & returns );
  392.  
  393.         virtual WBool SetText( const WString & str );
  394.  
  395.         virtual WString GetText() const;
  396.  
  397.         virtual WBool SetUserData( void * userData );
  398.  
  399.         virtual void * GetUserData() const;
  400.  
  401.         virtual WBool SetHeight( const WInt & height, WBool update=TRUE );
  402.  
  403.         virtual WInt GetHeight() const;
  404.  
  405.         virtual WStyle GetDefaultStyle() const;
  406.  
  407.         virtual WStyle GetDefaultExtendedStyle() const;
  408.  
  409.         virtual const WChar * InitializeClass();
  410.  
  411.         virtual WBool FDXIn();
  412.  
  413.         virtual WBool FDXOut();
  414.  
  415.         virtual WBool IgnoreDeleteItem();
  416.  
  417.         /**************************************************************
  418.          * Event Handlers
  419.          **************************************************************/
  420.  
  421.         // Event handlers for the DataTarget
  422.  
  423.         WBool DataOpenHandler( WListBox *, WDataOpenEventData *ev );
  424.         WBool DataCloseHandler( WListBox *, WDataCloseEventData *ev );
  425.         WBool DataAvailableHandler( WListBox *, WDataAvailableEventData *ev );
  426.         WBool DataRequestHandler( WListBox *, WDataRequestEventData *ev );
  427.         WBool DeleteItemHandler( WListBox *, WDeleteItemEventData *ev );
  428.         WBool DestroyEventHandler( WListBox *, WEventData * );
  429.         WBool DataLookupItemHandler( WListBox *, WDataLookupItemEventData *ev );
  430.         WBool DragEnterHandler( WListBox *, WDragEventData * ev );
  431.         WBool DragOverHandler( WListBox *, WDragEventData * ev );
  432.         WBool DragDropHandler( WListBox *, WDragEventData * ev );
  433.         WBool DragEndHandler( WListBox *, WDragEventData * ev );
  434.  
  435.         /**************************************************************
  436.          * Internal
  437.          **************************************************************/
  438.  
  439.     protected:
  440.  
  441.         WBool AllocDTColumn();
  442.         WBool AllocLookup();
  443.  
  444.         void * GetRealUserData( WInt itemNumber ) const;
  445.         WBool  SetRealUserData( WInt itemNumber, void * itemUserData );
  446.  
  447.         WBool IndirectUserData( WBool indirect );
  448.  
  449.         WBool DoLookupDataAvailable( WDataAvailableEventData *ev );
  450.         WBool DoColumnDataAvailable( WDataAvailableEventData *ev );
  451.  
  452.         void RenumberDataRows();
  453.         WBool HandleRefreshRow( WULong reason, WLong row, WInt count );
  454.         WBool AddDataRow( WLong row, WInt itemNumber = -1 );
  455.         WInt FindDataRow( WLong row, WInt itemCount = -1 );
  456.  
  457.         void RemoveLookupItems();
  458.  
  459.         /**************************************************************
  460.          * Data members
  461.          **************************************************************/
  462.  
  463.     protected:
  464.  
  465.         WInt                    _columnWidth;
  466.         WDataTargetColumn *     _dataTargetColumn;
  467.         WBool                   _indirectUserData;
  468.         WBool                   _dataFilling;
  469.         WBool                   _wasPainting;
  470.         WDataLookup *           _dataLookup;
  471.         WDataTargetCell *       _dataTargetCell;
  472.         WBool                   _bindAsLookup;
  473.         WBool                   _ignoreDataAvailableMove;
  474.         WInt                    _initialValue;
  475.         WBool                   _wasEnabled;
  476.         WInt                    _addRow;
  477.         WStringArray *          _fdxDataSourceStringArray;
  478.         WIntArray *             _fdxDataSourceIntArray;
  479.         WInt *                  _fdxDataSourceInt;
  480.         WBool *                 _fdxDataSourceBool;
  481.         WInt                    _itemToMove;
  482.         WBool                   _draggable;
  483.         WBool                   _userDataWasSet;
  484.         WBool                   _sendDeleteItem;
  485.         WBool                   _ignoreDeleteItem;
  486.  
  487.         // Indirections for databound controls...
  488.  
  489.         typedef WBool WCMDEF (WListBox::*CmdFuncPtr)( WUInt, WNotify,
  490.                                                       WNotifyInfo, WLong & );
  491.  
  492.         static CmdFuncPtr       DBProcessCommandPtr;
  493.  
  494.         WBool DBProcessCommand( WUInt id, WNotify code, WNotifyInfo info,
  495.                                 WLong & returns );
  496.  
  497.  
  498.         typedef WBool WCMDEF (WListBox::*SetIUDPtr)( WInt, void * );
  499.  
  500.         static SetIUDPtr        SetIndirectUserDataPtr;
  501.  
  502.         WBool SetIndirectUserData( WInt itemNumber, void * itemUserData );
  503.  
  504.         typedef void * WCMDEF (WListBox::*GetIUDPtr)( WInt );
  505.  
  506.         static GetIUDPtr        GetIndirectUserDataPtr;
  507.  
  508.         void *GetIndirectUserData( WInt itemNumber );
  509.  
  510.  
  511.         typedef void WCMDEF (WListBox::*AddIndirectPtr)( WInt, const WChar* );
  512.  
  513.         static AddIndirectPtr   DBAddIndirectPtr;
  514.  
  515.         void DBAddIndirect( WInt index, const WChar *text );
  516. };
  517.  
  518. #ifndef _WNO_PRAGMA_PUSH
  519. #pragma enum pop;
  520. #pragma pack(pop);
  521. #endif
  522.  
  523. #endif // _WLISTBOX_HPP_INCLUDED
  524.